package com.kly.cashmall.module.order.presenter;


import com.kly.cashmall.bean.LoanListBean;
import com.kly.cashmall.framework.mvp.BaseViewPresenter;
import com.kly.cashmall.services.Network;
import com.kly.cashmall.services.Params;
import com.kly.cashmall.utils.data.CollectionUtils;
import com.kly.cashmall.widget.Loading.LoadingDialog;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author
 */
public class LoanListPresenter extends BaseViewPresenter<LoanListViewer> {

    public LoanListPresenter(LoanListViewer viewer) {
        super(viewer);
    }

    @Override
    public void willDestroy() {

    }


    public void getInitialOrderList(String type, int currentPage) {
        LoadingDialog.showLoading(getActivity());
        Params params = new Params();
        params.put("type", type);
        params.put("currentPage", currentPage);
        rxManager.add(Network.getApi().getOrderList(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<LoanListBean>() {
            @Override
            public void accept(LoanListBean bean) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer() != null) {
                    if (currentPage == 1) {
                        if (CollectionUtils.isEmpty(bean.getRepaymentRecords())) {
                            getViewer().setOrderListFailRefreshing(bean.getNoticeMessage());
                        } else {
                            getViewer().setOrderListRefreshing(bean);
                        }
                    } else {
                        getViewer().setOrderListLoadmore(bean);
                    }
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer() != null) {
                    if (currentPage == 1) {
                        getViewer().setOrderListFailRefreshing("");
                    } else {
                        getViewer().setOrderListFailLoadmore();
                    }
                }

            }
        }));

    }
    public void getOrderList(String type, int currentPage) {
        Params params = new Params();
        params.put("type", type);
        params.put("currentPage", currentPage);
        rxManager.add(Network.getApi().getOrderList(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<LoanListBean>() {
            @Override
            public void accept(LoanListBean bean) throws Exception {
                if (getViewer() != null) {
                    if (currentPage == 1) {
                        if (CollectionUtils.isEmpty(bean.getRepaymentRecords())) {
                            getViewer().setOrderListFailRefreshing(bean.getNoticeMessage());
                        } else {
                            getViewer().setOrderListRefreshing(bean);
                        }
                    } else {
                        getViewer().setOrderListLoadmore(bean);
                    }
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if (getViewer() != null) {
                    if (currentPage == 1) {
                        getViewer().setOrderListFailRefreshing("");
                    } else {
                        getViewer().setOrderListFailLoadmore();
                    }
                }

            }
        }));
    }
}